package com.cays.myresearch.activity;

import android.content.Intent;
import android.os.Handler;
import android.os.Message;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.widget.Toast;

import com.cays.myresearch.R;
import com.cays.myresearch.Util.HttpUtil;
import com.cays.myresearch.action.LoginAction;
import com.cays.myresearch.adapter.ClassesAdapter;
import com.cays.myresearch.constant.Constant;
import com.cays.myresearch.entity.Classes;
import com.cays.myresearch.entity.ResponseInfo;
import com.cays.myresearch.entity.UserInfo;
import com.cays.myresearch.ui.HeadControlPanel;
import com.google.gson.reflect.TypeToken;

import java.io.IOException;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.MultipartBody;
import okhttp3.Response;

public class ClassActivity extends AppCompatActivity
        implements ClassesAdapter.ClassesClickListener,HeadControlPanel.HeadPanelCallback{
    private List<Classes> classInfoLists=new ArrayList<Classes>();
    private HeadControlPanel classHeader;
    private ClassesAdapter infoAdapter;
    private boolean myClass=false,myPayClass=false;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        myClass=getIntent().getBooleanExtra("my_class",false);
        myPayClass=getIntent().getBooleanExtra("my_pay_class",false);
        setContentView(R.layout.activity_class);
        initView();
        initClasses();
    }
    private void initView(){
        if (getSupportActionBar() != null){getSupportActionBar().hide();}
        if(classHeader==null){
            classHeader=(HeadControlPanel)findViewById(R.id.class_header);
            classHeader.initHeadPanel();
            classHeader.setHeadPanelCallback(this);
        }
        classHeader.setLeftImage(R.drawable.ic_back);
        if (myClass){
            classHeader.setMiddleTitle("我的课程");
        }else if (myPayClass){
            classHeader.setMiddleTitle("我购买的课程");
        }else {
            classHeader.setMiddleTitle("课程列表");
        }
        classHeader.setRightTitle("");
    }
    private void initClasses(){
        classInfoLists.clear();
        RecyclerView recyclerView1=(RecyclerView)findViewById(R.id.class_info_list);
        GridLayoutManager layoutManager=new GridLayoutManager(ClassActivity.this,1);
        recyclerView1.setLayoutManager(layoutManager);
        infoAdapter=new ClassesAdapter(classInfoLists);
        recyclerView1.setAdapter(infoAdapter);
        infoAdapter.setmClassesListener(this);
        refreshClasses();
    }
    private Handler handler=new Handler(new Handler.Callback() {
        @Override
        public boolean handleMessage(Message msg) {
            if (msg.what==0){
                classInfoLists.clear();
            }else if(msg.what==1){
                String s=(String) msg.obj,info="",classes="";
                if (s.contains("~")) {
                    info=s.split("~")[0];
                    classes=s.split("~")[1];
                    List<Classes> classList=Constant.gson.fromJson(classes,
                            new TypeToken<List<Classes>>(){}.getType());
                    classInfoLists.clear();
                    if(!classList.isEmpty())classInfoLists.addAll(classList);
                }
            }
            infoAdapter.notifyDataSetChanged();
            return true;
        }
    });
    private void refreshClasses(){
        final String url;
        if (myClass){
            url= Constant.URL+"class/my_class";
            LoginAction action=new LoginAction();
            if (!action.hasUserInfo(getApplicationContext())){
                Toast.makeText(this,"未登录",Toast.LENGTH_SHORT).show();
                return;
            }
            UserInfo userInfo=action.getUserInfo(getApplicationContext());
            final MultipartBody.Builder builder=new MultipartBody.Builder();
            builder.addFormDataPart("name",userInfo.getName());
            HttpUtil.sendHttpRequest(url,builder,handler,1,0);
        }else if (myPayClass){
            url= Constant.URL+"class/my_pay_class";
            LoginAction action=new LoginAction();
            if (!action.hasUserInfo(getApplicationContext())){
                Toast.makeText(this,"未登录",Toast.LENGTH_SHORT).show();
                return;
            }
            UserInfo userInfo=action.getUserInfo(getApplicationContext());
            final MultipartBody.Builder builder=new MultipartBody.Builder();
            builder.addFormDataPart("name",userInfo.getName());
            HttpUtil.sendHttpRequest(url,builder,handler,1,0);
        }else{
            url= Constant.URL+"class/list";
            HttpUtil.sendHttpRequest(url,handler,1,0);
        }

    }
    @Override
    public void onClassesClick(int position) {
        Classes info=classInfoLists.get(position);
        Intent intent=new Intent(ClassActivity.this, ClassInfoActivity.class);
        intent.putExtra("intent_class", Constant.gson.toJson(info));
        startActivity(intent);
    }

    @Override
    public void onHeadPanelClick(int itemId) {
        switch (itemId){
            case Constant.btn_left_title:
                finish();
                break;
            case Constant.btn_middle_title:
                refreshClasses();
                break;
            case Constant.btn_right_title:
                refreshClasses();
                break;
            default:break;
        }
    }
}
